package cn.itsource.xhy.org.service.impl;

import cn.itsource.xhy.basic.service.impl.BaseServiceImpl;
import cn.itsource.xhy.org.domain.Employee;
import cn.itsource.xhy.org.mapper.EmployeeMapper;
import cn.itsource.xhy.org.service.IEmployeeService;
import cn.itsource.xhy.user.domain.LoginInfo;
import cn.itsource.xhy.user.mapper.LoginInfoMapper;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void findAndUpdate(Long id) {
        Employee emp = employeeMapper.findById(id);
        emp.setState(1);
        employeeMapper.update(emp);


    }

    @Override
    public LoginInfo login(Employee employee) {
        // 验证用户名是否正确
        LoginInfo loginInfo = loginInfoMapper.login(employee.getUsername());
        if (loginInfo == null) {
            throw new RuntimeException("1用户名或密码错误");
        }
        // 验证密码 输入的密码加盐加密
        String md5 = MD5Utils.encrypByMd5(employee.getPassword() + loginInfo.getSalt());
        if (!md5.equals(loginInfo.getPassword())){
            throw new RuntimeException("2用户名或密码错误");
        }
        return loginInfo;
    }
}
